#include "test_spi.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "cm_iomux.h"
#include "cm_mem.h"
#include "string.h"


// void cm_test_spi_open_close(unsigned char *cmd[],int len)
// {
//     if(0 == strcmp("open",cmd[2]))
//     {
//         cm_spi_dev_e dev = atoi(cmd[3]);
//         cm_spi_mode_e mode = atoi(cmd[4]);
//         cm_spi_work_mode_e work_mode = atoi(cmd[5]);
//         cm_spi_data_width_e data_width = atoi(cmd[6]);
//         cm_spi_nss_e nss = atoi(cmd[7]);
//         int clk = atoi(cmd[8]);
//         cm_spi_cfg_t cfg = 
//         {
//             mode,  /** SPI主从模式 */
//             work_mode, /** 模式 */
//             data_width,  /** SPI传输数据宽度 */
//             nss,/** SPI的NSS管理模式 */
//             clk, /** 时钟频率*/
//         };
//         cm_demo_printf("spi cm_spi_open ret:%d\n", cm_spi_open(dev, &cfg));
//     }
//     else if(0 == strcmp("close",cmd[2]))
//     {
//         cm_spi_dev_e dev = atoi(cmd[3]);
//         cm_spi_close(dev);
//     }
//     else
//     {
//         cm_demo_printf("param error!\n");
//     }
// }
#define TRAVERSAL_SIZE_T  4096
static int TRAVERSAL_SIZE = 4096;
char traversal_wdata[TRAVERSAL_SIZE_T] = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234";
void cm_test_spi_operation(unsigned char *cmd[],int len)
{
    if(0 == strcmp("OPEN",cmd[2]))
    {
        cm_spi_dev_e dev = atoi(cmd[3]);
        cm_spi_mode_e mode = atoi(cmd[4]);
        cm_spi_work_mode_e work_mode = atoi(cmd[5]);
        cm_spi_data_width_e data_width = atoi(cmd[6]);
        cm_spi_nss_e nss = atoi(cmd[7]);
        int clk = atoi(cmd[8]);
        cm_spi_cfg_t cfg = 
        {
            mode,  /** SPI主从模式 */
            work_mode, /** 模式 */
            data_width,  /** SPI传输数据宽度 */
            nss,/** SPI的NSS管理模式 */
            clk, /** 时钟频率*/
        };
        int32_t spi_open = cm_spi_open(dev, &cfg);
        cm_demo_printf("spi cm_spi_open ret:%d\n",spi_open);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,spi_open,cm_spi_open);
    }
    else if(0 == strcmp("CLOSE",cmd[2]))
    {
        cm_spi_dev_e dev = atoi(cmd[3]);
        int32_t spi_close = cm_spi_close(dev);
        cm_demo_printf("spi cm_spi_close ret:%d\n",spi_close);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,spi_close,cm_spi_close);
    }
    else if (0 == strcmp("WRITE",cmd[2]))
    {
        cm_spi_dev_e dev = atoi(cmd[3]);
        int32_t data_len = atoi(cmd[4]);
        uint8_t *data = (uint8_t *)cm_malloc(data_len*sizeof(uint8_t));
        memset(data,0,data_len*sizeof(uint8_t));
        data[0] = strtoul(cmd[5],(void**)0,16);//cmd
        data[1] = strtoul(cmd[6],(void**)0,16);//addr1
        data[2] = strtoul(cmd[7],(void**)0,16);//addr2
        data[3] = strtoul(cmd[8],(void**)0,16);//addr3
        //cm_demo_printf("data0123%#x,%#x,%#x,%#x\n",data[0],data[1],data[2],data[3]);

        if(data_len>4)
        {
            memcpy(data+4,cmd[9],strlen(cmd[9]));
        }
        char w_cmd[] = {0x06};//写使能命令
	    osDelay(10);
        cm_spi_write(dev, w_cmd, 1);
        osDelay(10);
        int32_t spi_write = cm_spi_write(dev,data,data_len);
        cm_demo_printf("spi cm_spi_write ret:%d\n",spi_write);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,spi_write,cm_spi_write);
        cm_demo_printf("%s\n",data+4);
        osDelay(200);
        cm_free(data);
    }else if(0 == strcmp("READ",cmd[2]))
    {
        cm_spi_dev_e dev = atoi(cmd[3]);
        char data[10] = {0};
        int32_t len = 3;
        char w_cmd[] = {0x9f,0,0,0};//读取id命令
        //cm_spi_write(dev,w_cmd,4);
        osDelay(1);
        int32_t spi_read = cm_spi_read(dev,data,len);
        cm_demo_printf("cm_spi_read ret:%d\n",spi_read);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,spi_read,cm_spi_read);
        cm_demo_printf("read data:%#2x,%#2x,%#2x\n",data[0],data[1],data[2]);
    }
    else if(0 == strcmp("WRITE_READ",cmd[2]))
    {
        cm_spi_dev_e dev = atoi(cmd[3]);
        int32_t w_len = atoi(cmd[4]);
        uint8_t *w_data = (uint8_t *)cm_malloc(w_len*sizeof(uint8_t));

        w_data[0] = strtoul(cmd[5],(void**)0,16);//cmd
        w_data[1] = strtoul(cmd[6],(void**)0,16);//addr1
        w_data[2] = strtoul(cmd[7],(void**)0,16);//addr2
        w_data[3] = strtoul(cmd[8],(void**)0,16);//addr3
        //cm_demo_printf("w_data0123%#x,%#x,%#x,%#x\n",w_data[0],w_data[1],w_data[2],w_data[3]);
        char r_data[1000] = {0};
        int32_t r_len = atoi(cmd[9]);//read len
        int32_t spi_write_then_read = cm_spi_write_then_read(dev,w_data,w_len,r_data,r_len);
        cm_demo_printf("spi cm_spi_write_then_read ret:%d\n",spi_write_then_read);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,spi_write_then_read,cm_spi_write_then_read);
        cm_demo_printf("read data:%s\n",r_data);
        for(int i = 0;i<r_len;i++)
        {
            cm_demo_printf("%#2x,",r_data[i]);
        }
        cm_demo_printf("\n");
        osDelay(200);
        cm_free(w_data);
    }else if(0 == strcmp("TRAVERSAL",cmd[2]))
    {
        cm_spi_dev_e dev = atoi(cmd[3]);
        TRAVERSAL_SIZE = atoi(cmd[4]);
        int32_t w_len = 4;
        uint8_t w_data[4] = {3,0,0,0};//cmd addr1,2,3
        char w_cmd[] = {0x06};//写使能命令
        uint8_t status = 1;
        traversal_wdata[0] = 2;
        traversal_wdata[1] = 0;
        traversal_wdata[2] = 0;
        traversal_wdata[3] = 0;
        
        // cm_spi_write(dev, w_cmd, 1);//写使能
        // osDelay(10);
        // cm_spi_write(dev,(char []){0x20,0,0,0},4);//擦除sector
        // osDelay(400);
        int traversal_wlen = 0;
        for(int i = 1;i <= TRAVERSAL_SIZE;++i)
        {
            // cm_spi_write(dev, w_cmd, 1);//写使能
            // osDelay(20);
            // cm_spi_write(dev,(char []){0x20,0,0,0},4);//擦除sector
            // osDelay(200);
            osDelay(10);
            cm_spi_write(dev, w_cmd, 1);//写使能
            osDelay(10);
            traversal_wlen = i + 4;
            int32_t spi_write = cm_spi_write(dev,traversal_wdata,traversal_wlen);//写数据
            cm_demo_printf("spi cm_spi_write ret:%d\n",spi_write);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,spi_write,cm_spi_write);
            osDelay(50);
            // while((uint8_t)1 == (uint8_t)((uint8_t)0x01 & status))
            // {
            //     osDelay(20);
            //     static int is_busy_count = 0;
            //     cm_spi_write_then_read(dev,0x5,1,&status,1);//读取读取spi flash寄存器状态
            //     is_busy_count += 1;
            //     if(is_busy_count > 200)
            //     {
            //         cm_demo_printf("read flash status fail, status:%d\n",status);
            //         is_busy_count = 0;
            //         break;
            //     }
            // }
            
            //osDelay(100);
        }
        char traversal_rdata[TRAVERSAL_SIZE_T] = {0};
        int32_t traversal_rlen = 0;//read len
        for(int i = 1;i <= TRAVERSAL_SIZE;++i)
        {
            traversal_rlen = i;
            int32_t spi_write_then_read = cm_spi_write_then_read(dev,w_data,w_len,traversal_rdata,traversal_rlen);
            cm_demo_printf("spi cm_spi_write_then_read ret:%d\n",spi_write_then_read);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,spi_write_then_read,cm_spi_write_then_read);
            cm_demo_printf("read len:%d\n",traversal_rlen);
            for(int j = 0;j<traversal_rlen;j++)
            {
                cm_demo_printf("%#2x,",traversal_rdata[j]);
            }
            cm_demo_printf("\n------------------------\n");
            osDelay(50);
        }
        //cm_free(w_data);
    }
    // else if(0 == strcmp("TRANS_MSG",cmd[2]))
    // {
    //     cm_spi_dev_e dev = atoi(cmd[3]);
    //     bool cs_ctrl = atoi(cmd[4]);       /*!< 是否在传输完成后失能片选信号*/
    //     int w_len = strlen(cmd[5]);
    //     uint8_t *w_data = (uint8_t *)malloc(w_len*sizeof(uint8_t)/2);
    //     char temp[2] = {0};
    //     cm_demo_printf("write data:");
    //     for(int i = 0;i<w_len;i=i+2)
    //     {
    //         temp[0] = cmd[5][i];
    //         temp[1] = cmd[5][i+1];
    //         w_data[i/2] = (uint8_t)strtoul(temp,(void**)0,16);
    //         cm_demo_printf("%#02x ",w_data[i/2]);
    //     }
    //     cm_demo_printf("\n");
    //     char r_data[100] = {0};
    //     int r_len = atoi(cmd[6]);
    //     cm_spi_msg_t msg = {
    //         cs_ctrl,       /*!< 是否在传输完成后失能片选信号*/
    //         w_data,
    //         r_data,
    //         w_len/2,
    //         r_len,
    //     };
    //     cm_demo_printf("spi cm_spi_transfer_msg ret:%d\n", cm_spi_transfer_msg(dev, &msg));
    //     cm_demo_printf("read data:");
    //     for(int i = 0;i<msg.r_len;++i)
    //     {
    //         cm_demo_printf("%#02x ",r_data[i]);
    //     }
    //     cm_demo_printf("\n");
    // }
    else{
        cm_demo_printf("param error!\n");
    }
    
        
}
